package com.example.tests;

import java.io.FileReader;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;


import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import com.example.fw.ApplicationManager;
import com.example.fw.GroupObject;

public class TestBase {
	
	protected Logger log = Logger.getLogger("TEST");
	
	protected ApplicationManager app;

	@BeforeClass
	@Parameters({"configFile"})
	public void setUp(@Optional String configFile) throws Exception {
		if (configFile == null) {
			configFile = System.getProperty("configFile");
		}
		if (configFile == null) {
			configFile = System.getenv("configFile");
		}
		if (configFile == null) {
			configFile = "application.properties";
		}
		Properties props = new Properties();
		props.load(new FileReader(configFile));
		log.info("setUp start");
		app = ApplicationManager.getInstance();
		app.setProperties(props); 
		log.info("setUp end");
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		log.info("tearDown start");
		ApplicationManager.getInstance().stop();
		log.info("tearDown end");
	}

	protected void verifyGroupAdded(Set<GroupObject> oldList, GroupObject validGroup, Set<GroupObject> newList) {
		Assert.assertEquals(newList.size(), oldList.size()+1);
		validGroup.setId("$$$");
		oldList.add(validGroup);
		validGroup.setId(null);
		Assert.assertEquals(newList, oldList);
	}
	
	protected void verifyGroupModified(Set<GroupObject> oldList, GroupObject targetGroup, GroupObject modifiedGroup, Set<GroupObject> newList) {
		 Assert.assertEquals(newList.size(), oldList.size());
		 oldList.remove(targetGroup);
		 modifiedGroup.setId("###");
		 oldList.add(modifiedGroup);
		 modifiedGroup.setId(null);
		 Assert.assertEquals(newList, oldList);
		 }
	
	protected void verifyGroupDeleted(Set<GroupObject> oldList, GroupObject group, Set<GroupObject> newList) {
		Assert.assertEquals(newList.size(), oldList.size()-1);
		oldList.remove(group);
		Assert.assertEquals(newList, oldList);
		}

	protected void verifyContactAdded(Set<ContactObject> oldList, ContactObject validContact, Set<ContactObject> newList) {
		Assert.assertEquals(newList.size(), oldList.size()+1);
		validContact.setId("$$$");
		oldList.add(validContact);
		validContact.setId(null);
		Assert.assertEquals(newList, oldList);
	}

	protected void verifyContactModified(Set<ContactObject> oldList, ContactObject targetContact, ContactObject modifiedContact, Set<ContactObject> newList) {
		Assert.assertEquals(newList.size(), oldList.size());
		oldList.remove(targetContact);
		modifiedContact.setId("###");
		oldList.add(modifiedContact);
		modifiedContact.setId(null);
		Assert.assertEquals(newList, oldList);
	}
	
	protected void verifyContactRemoved(Set<ContactObject> oldList, ContactObject contact, Set<ContactObject> newList) {
		Assert.assertEquals(newList.size(), oldList.size()-1);
		oldList.remove(contact);
		Assert.assertEquals(newList, oldList);
	}

}
